package gaobo.linkedlist;

import gaobo.singlelist.IndexWrongFulException;

//双向链表
public class MyLinkedList {

    //节点内部类
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//标记双向链表的头部
    public ListNode tail;//标记双向链表的尾部

    //打印双向链表
    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到双向链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    //查找是否包含关键字key在链表中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data){
    //1.判断index位置的合法性
    if (index < 0 || index > size()){
        throw new IndexWrongFulException("index位置不合法");
    }
    //2.判断特殊位置，头插 和 尾插
    if (index == 0){
        addFirst(data);
        return;
    }
    if (index == size()){
        addLast(data);
        return;
    }
    //3.找到index位置节点的地址
    ListNode cur = findIndexListNode(index);
    //4.修改4个指向
    ListNode node = new ListNode(data);
    node.next = cur;
    cur.prev.next = node;
    node.prev = cur.prev;
    cur.prev = node;
    }
    private ListNode findIndexListNode(int index){
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一个关键字为Key的节点
    public void remove(int key){
        ListNode cur = head;

        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    //考虑只有一个节点
                    if (head != null){
                        head.prev = null;
                    }else {
                        tail = null;
                        System.out.println("链表为空");
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        this.tail = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key
    public void removeAllKey(int key){
        ListNode cur = head;

        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    //考虑只有一个节点
                    if (head != null){
                        head.prev = null;
                    }else {
                        tail = null;
                        System.out.println("链表为空");
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    //清空双向链表
    //只能一个节点一个节点置空
    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
         head = null;
         tail = null;
        System.out.println("列表已清空");
    }



    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
//        myLinkedList.addFirst(1);
//        myLinkedList.addFirst(2);
//        myLinkedList.addFirst(3);
//        myLinkedList.addFirst(4);
//        myLinkedList.addFirst(5);
        myLinkedList.addLast(1);
        myLinkedList.addLast(1);
        myLinkedList.addLast(3);
        myLinkedList.addLast(1);
        myLinkedList.addLast(5);
        myLinkedList.display();

//        myLinkedList.addIndex(0,888);
//        myLinkedList.display();
        //myLinkedList.remove(1);
        //myLinkedList.removeAllKey(1);
        myLinkedList.clear();
        myLinkedList.display();
    }


}
